package me.zk.practice1.s;

import me.zk.practice1.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author: zhang kai
 * @Date: 2023/04/27
 * @Description:
 */
public class S4 {

    public static void main(String[] args) {
        TreeNode t = new TreeNode(3,new TreeNode(9),new TreeNode(20,new TreeNode(15),new TreeNode(7)));
        System.out.println(minDepth(t));
    }
    public static int minDepth(TreeNode root) {
        if (root==null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(root);

        int depth =1;
        while (!queue.isEmpty()) {
            //将左右的关系入队
            int sc = queue.size();
            for (int i=0; i< sc;i++) {
                TreeNode cur = queue.poll();
                if (cur.left == null && cur.right==null) {
                    return depth;
                }
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            depth++;
        }
        return depth;
    }

}